import { getOrderCheckout } from "@/store/userDataContext";
import { OrderShape } from "@/data/order/orderTypes";
import { useAuth } from "@/store/authContext";
import Taro, { useRouter } from "@tarojs/taro";
import { useEffect, useState } from "react";

export default function useOrderCheckoutData() {
  const { dispatchAuthAction } = useAuth();
  const [orderCheckoutData, setOrderCheckoutData] =
    useState<null | OrderShape>();
  const { params } = useRouter();
  useEffect(() => {
    const fetchData = async () => {
      Taro.showLoading();
      if (params.preOrderNo) {
        const detailData = (await getOrderCheckout({
          query: { preOrderNo: params.preOrderNo },
          authDispatch: dispatchAuthAction,
        })) as unknown as any;
        detailData.preOrderCommonVo.preOrderNo = params.preOrderNo;
        setOrderCheckoutData(detailData);
      }
      Taro.hideLoading();
    };
    fetchData();
  }, []);

  return [
    orderCheckoutData?.preOrderCommonVo,
    orderCheckoutData?.publicationRecommendations,
  ];
}
